package com.ruoyi.robot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.robot.pojo.po.RobotType;
import com.ruoyi.robot.mapper.RobotTypeMapper;
import com.ruoyi.robot.pojo.dto.RobotTypeDto;
import com.ruoyi.robot.service.RobotTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.robot.util.DtoToPoMapper;
import com.ruoyi.robot.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yid
 * @since 2023-01-16
 */
@Service
public class RobotTypeServiceImpl extends ServiceImpl<RobotTypeMapper, RobotType> implements RobotTypeService {

    private final RobotTypeMapper robotTypeMapper;

    @Autowired
    public RobotTypeServiceImpl(RobotTypeMapper robotTypeMapper) {
        this.robotTypeMapper = robotTypeMapper;
    }

    /**
     * 查询机器人类型
     *
     * @param typeId 机器人类型主键
     * @return 机器人类型
     */
    @Override
    public RobotType selectRobotTypeByTypeId(Long typeId)
    {
        return robotTypeMapper.selectById(typeId);
    }

    /**
     * 查询机器人类型列表
     *
     * @param robotTypeDto 机器人类型
     * @return 机器人类型
     */
    @Override
    public List<RobotType> selectRobotTypeList(RobotTypeDto robotTypeDto)
    {
        RobotType robotType = DtoToPoMapper.INSTANCE.toRobotType(robotTypeDto);
        return robotTypeMapper.selectRobotTypeList(robotType);
    }

    /**
     * 新增机器人类型
     *
     * @param robotTypeDto 机器人类型
     * @return 结果
     */
    @Override
    public AjaxResult insertRobotType(RobotTypeDto robotTypeDto)
    {
        RobotType robotType = DtoToPoMapper.INSTANCE.toRobotType(robotTypeDto);
        QueryWrapper<RobotType> wrapper = new QueryWrapper<>();
        wrapper.eq("type",robotType.getType());
        if(robotTypeMapper.selectCount(wrapper) != 0){
            return AjaxResult.error("该型号已经存在");
        }

        return robotTypeMapper.insert(robotType) > 0?AjaxResult.success("新增成功"):AjaxResult.error("新增失败");

    }

    /**
     * 修改机器人类型
     *
     * @param robotTypeDto 机器人类型
     * @return 结果
     */
    @Override
    public int updateRobotType(RobotTypeDto robotTypeDto)
    {
        RobotType robotType = DtoToPoMapper.INSTANCE.toRobotType(robotTypeDto);
        return robotTypeMapper.updateById(robotType);
    }

    /**
     * 批量删除机器人类型
     *
     * @param typeIds 需要删除的机器人类型主键
     * @return 结果
     */
    @Override
    public int deleteRobotTypeByTypeIds(Long[] typeIds)
    {
        return robotTypeMapper.deleteRobotTypeByTypeIds(typeIds);
    }

    /**
     * 删除机器人类型信息
     *
     * @param typeId 机器人类型主键
     * @return 结果
     */
    @Override
    public int deleteRobotTypeByTypeId(Long typeId)
    {
        return robotTypeMapper.deleteById(typeId);
    }

    @Override
    public AjaxResult uploadPicture(MultipartFile file) {
        if(file == null){
            return AjaxResult.error("上传文件不能为空");
        }
        String robotUrl = FileUploadUtil.upload(file, Constants.ROBOT_BASE_DIR);
        String robot = Constants.ROBOT_RETURN_BASE_DIR+robotUrl;
        return AjaxResult.success("上传成功",robot);
    }

}
